from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
import os

class MaterialInfo(models.Model):
    STATUS_CHOICES = (
        ('待开始', '待开始'),
        ('报价中', '报价中'),
        ('已报价', '已报价'),
        ('建料中', '建料中'),
        ('已成交', '已成交'),
        ('已取消', '已取消'),
        ('已完成', '已完成'),
    )

    salesperson = models.CharField(verbose_name='业务员', max_length=50)
    original_pn = models.CharField(verbose_name='原厂料号', max_length=50, blank=True)
    fab_pn = models.CharField(verbose_name='Fab料号', max_length=50, blank=True)
    new_pn = models.CharField(verbose_name='新料号', max_length=50, blank=True)
    status = models.CharField(verbose_name='成交状态', max_length=20, choices=STATUS_CHOICES, default='待开始', blank=True)
    customer = models.CharField(verbose_name='客户', max_length=100, blank=True)
    machine_model = models.CharField(verbose_name='机台型号', max_length=100, blank=True)
    category = models.CharField(verbose_name='类别', max_length=50, blank=True)
    department = models.CharField(verbose_name='部门', max_length=50, blank=True)
    oem = models.CharField(verbose_name='OEM', max_length=50, blank=True)
    product_info = models.TextField(verbose_name='产品信息', blank=True)
    size_standard = models.CharField(verbose_name='尺寸标准', max_length=50, blank=True)
    size = models.CharField(verbose_name='尺寸', max_length=50, blank=True)
    pcs_per_chamber = models.CharField(verbose_name='pcs/chamber', max_length=50, blank=True)
    original_usage = models.TextField(verbose_name='原厂使用说明', blank=True)
    material = models.CharField(verbose_name='材质', max_length=100, blank=True)
    current_material = models.CharField(verbose_name='当前使用材料', max_length=100, blank=True)
    current_supplier = models.CharField(verbose_name='当前供应商', max_length=100, blank=True)
    usage_cycle = models.CharField(verbose_name='使用周期', max_length=100, blank=True)
    replacement_material = models.CharField(verbose_name='替换材质', max_length=100, blank=True)
    currency = models.CharField(verbose_name='报价币种', max_length=10, default='CNY', blank=True)
    unit_price = models.CharField(verbose_name='单价（报价）', max_length=50, blank=True)
    created_at = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    updated_at = models.DateTimeField(verbose_name='更新时间', auto_now=True)
    # 确保created_by字段存在
    created_by = models.ForeignKey(
        User,
        on_delete=models.SET_NULL,
        related_name='created_materials',
        null=True,
        editable=False,
        verbose_name='创建人'
    )
    updated_by = models.ForeignKey(
        User,
        on_delete=models.SET_NULL,
        related_name='updated_materials',
        null=True,
        editable=False,
        verbose_name='更新人'
    )
 
    def save(self, *args, **kwargs):
        """可选：补充保存逻辑（如果未在admin中覆盖save_model）"""
        if not self.pk:
            self.created_by = self.created_by or kwargs.get('user')
        self.updated_by = self.updated_by or kwargs.get('user')
        return super().save(*args, **kwargs)
    
    class Meta:
        verbose_name = "资料信息"
        verbose_name_plural = "资料信息"
        indexes = [
            models.Index(fields=['salesperson', 'original_pn', 'customer']),
            models.Index(fields=['created_by']),
        ]
        
    def __str__(self):
        return f"{self.original_pn} - {self.customer}"

class ProductInfo(models.Model):  
    product_code = models.CharField(verbose_name='产品代码', max_length=50)  
    product_name = models.CharField(verbose_name='产品名称', max_length=100)  
    specification = models.TextField(verbose_name='规格说明', blank=True)  
    price = models.DecimalField(verbose_name='价格', max_digits=10, decimal_places=2)  
    created_at = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)  
    created_by = models.ForeignKey(  
        User,  
        on_delete=models.SET_NULL,  
        related_name='created_products',  
        null=True,  
        editable=False,  
        verbose_name='创建人'  
    )  
      
    class Meta:  
        verbose_name = "产品信息"  
        verbose_name_plural = "产品信息"  
      
    def __str__(self):  
        return f"{self.product_code} - {self.product_name}"  
  
